﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.DALFactory;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// Cms解析基类
    /// </summary>
    public class CommonModel : ITemplateType
    {
        //protected IStaticGeneratableModel vo;
        public IStaticGeneratableModel Model { get; set; }

        //栏目相关
        protected MenuModel menupage;
        public MenuModel MenuModel
        {
            get { return menupage; }
        }

        //公共方法相关
        protected ContentHtmlPage htmlpage;
        protected string html = ""; //模板HTML

        public CommonModel() { }

        public CommonModel(IStaticGeneratableModel model)
        {
            this.Model = model;
            if (model != null && model.TemplateID.HasValue)
            {
                this.html = new ModelManageBLL().getModel(model.TemplateID.Value).TemCotent;
                if (model.ColumnID > 0)
                {//存在栏目ID
                    menupage = new MenuModel(model.ColumnID.Value, model.ID.Value);
                }
                else
                {
                    menupage = new MenuModel();
                }
            }
            else
            {
                menupage = new MenuModel();
            }
            htmlpage = new ContentHtmlPage(menupage);
        }

        /// <summary>
        /// Cms 模板标签正则定义 形如: <cms:content value="GetTest"></cms:content>
        /// </summary>
        static Regex regex = new Regex("<cms:\\w+\\s+value=[\'|\"][\\w\\.\\s\\*]+[\'|\"][\\s\\w\'\"\\=]*>[\\[\\]\\&\\;\\!\'\"\\=<>/\\w\\s]*</cms:\\w+>");

        public virtual string GetHtml()
        {
            MatchCollection mc = regex.Matches(html);

            foreach (Match m in mc)
            {
                string mv = m.Value;
                html = StringUtil.ReplaceStr(html, mv, LabelProxy.GetCmsLabel(this, mv).GetLabelHtml());
            }

            return html;
        }

        //title/meta/seo
        public virtual string GetMeta()
        {
            StringBuilder sb = new StringBuilder();
            ColumnModel cm = menupage.GetColumn(); //当前栏目

            if (this.Model.SEOMetaKeywords != null && this.Model.SEOMetaKeywords.Trim() != "")
            {//有设定
                sb.Append("<meta name=\"Keywords\" content=\"");
                sb.Append(this.Model.SEOMetaKeywords);
                sb.Append("\" />\n");

                sb.Append("<meta name=\"Description\" content=\"");
                sb.Append(this.Model.SEOMetaDescription);
                sb.Append("\" />\n");
            }
            else
            {
                //栏目SEO
                sb.Append(menupage.GetColumntSEO(true));
            }
            
            return sb.ToString();
        }

        public virtual string GetCnHeaderNavi()
        {
            return menupage.GetHeaderNavi(MenuModel.CnHomeColumnID);
        }

        public virtual string GetCnLeftNavi()
        {
            return menupage.GetLeftNavi(MenuModel.CnHomeColumnID);
        }

        public virtual string GetCnPositionNavi()
        {
            return menupage.GetPositionNavi(MenuModel.CnHomeColumnID);
        }

        public virtual string GetEnHeaderNavi()
        {
            return menupage.GetHeaderNavi(MenuModel.EnHomeColumnID);
        }

        public virtual string GetEnLeftNavi()
        {
            return menupage.GetLeftNavi(MenuModel.EnHomeColumnID);
        }

        public virtual string GetEnPositionNavi()
        {
            return menupage.GetPositionNavi(MenuModel.EnHomeColumnID);
        }
        public virtual string GetColumnSEOTitle()
        {
            if (string.IsNullOrEmpty(this.Model.SEOTitle)) return "";
            return this.Model.SEOTitle;
        }

    }
}
